package com.leyou.service;


import com.leyou.clients.GoodsClient;
import com.leyou.clients.SpecClient;
import item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: taft
 * @Date: 2018-11-26 10:21
 */
@Service
public class PageService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    //TODO  根据spuId查Spu，spuDetail，skus，以及所有的规格参数


    public Map<String, Object> loadModel(Long id) {

        Map<String,Object> map = new HashMap<>();

        //SPU
        Spu spu = goodsClient.querySpuById(id);

        map.put("spu",spu);

        //SPUDetail
        SpuDetail spuDetail = goodsClient.querySpuDetailById(id);

        map.put("spuDetail",spuDetail);

        //skus
        List<Sku> skus = goodsClient.querySkuBySpuId(id);

        map.put("skus",skus);

        //规格参数组，所有的
        List<SpecGroup> specGroups = specClient.querySpecGroups(spu.getCid3());

        map.put("specGroups",specGroups);

        //所有的特有规格参数
        List<SpecParam> specParams = specClient.querySpecParam(null, spu.getCid3(), null, false);


        // 处理成id:name格式的键值对
        Map<Long,String> paramMap = new HashMap<>();

        for (SpecParam specParam : specParams) {
            paramMap.put(specParam.getId(),specParam.getName());//4:机身颜色
        }

        map.put("paramMap",paramMap);

        return map;
    }
}
